package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

import com.anhuanjia.tools.eao.bean.EaoHatArea;
import com.anhuanjia.tools.eao.bean.EaoHatCity;
import com.anhuanjia.tools.eao.bean.EaoHatProvince;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;

/**
 * 三级联查操作
 * 
 * @author Administrator
 *
 */

@Alias(value = "provinceCityArea")
public class ProvinceCityAreaUI extends UIFacade {

	/**
	 * --省
	 */
	@Priv(loginType = LoginType.Member)
	public void findProvince() {
		DataTable dt = new Q(" select * from eao_hat_province").fetch();
		List<EaoHatProvince> list = new ArrayList<>();
		for (DataRow dataRow : dt) {
			EaoHatProvince hatProvince = new EaoHatProvince();
			hatProvince.setValue(dataRow);
			list.add(hatProvince);
		}
		this.Response.put("HatProvince", list);
	}
	
	/**
	 * --市
	 */
	@Priv(loginType = LoginType.Member)
	public void findCity() {
		String provinceId = $V("ProvinceId");
		DataTable dt = new Q(" select * from eao_hat_city where Father = ?" , provinceId).fetch();
		List<EaoHatCity> list = new ArrayList<>();
		for (DataRow dataRow : dt) {
			EaoHatCity hatCity = new EaoHatCity();
			hatCity.setValue(dataRow);
			list.add(hatCity);
		}
		this.Response.put("HatCity", list);
	}
	
	/**
	 * --区/县
	 */
	@Priv(loginType = LoginType.Member)
	public void findArea() {
		String cityId = $V("City");
		DataTable dt = new Q(" select * from eao_hat_area where Father = ?" , cityId).fetch();
		List<EaoHatArea> list = new ArrayList<>();
		for (DataRow dataRow : dt) {
			EaoHatArea hatArea = new EaoHatArea();
			hatArea.setValue(dataRow);
			list.add(hatArea);
		}
		this.Response.put("HatArea", list);
	}
	
	/**
	 * --根据地址获取邮编
	 */
	@Priv(loginType = LoginType.Member)
	public void findCode() {
		String Province = $V("Province");
		String City = $V("City");
		String Area = $V("Area");
		String code = null;
		Boolean is = false;
		Q q = new Q("select PostCode from eao_post_code where 1 = 1 ");
		if(!StringUtils.isEmpty(Province) && !StringUtils.isEmpty(City) && !StringUtils.isEmpty(Area)) {
			DataTable dataTable = new Q("select Area from eao_post_code where 1 = 1 and Province like ? and Ctiy like ?", "%"+Province+"%", "%"+City+"%").fetch();
			for (DataRow dataRow : dataTable) {
				if(Area.equals(dataRow.get(0))) {
					is = true;
				}
			}
			if(is) {
				q.append(" and Province like ? and Ctiy like ? and Area like ?", "%"+Province+"%", "%"+City+"%", "%"+Area+"%");
				DataTable dt = q.fetch();
				code = dt.getString(0, 0);
			} else {
				Area = City;
				q.append(" and Province like ? and Ctiy like ? and Area like ?", "%"+Province+"%", "%"+City+"%", "%"+Area+"%");
				DataTable dt = q.fetch();
				code = dt.getString(0, 0);
			}
		} else if(!StringUtils.isEmpty(Province) && !StringUtils.isEmpty(City) && StringUtils.isEmpty(Area)) {
			Area = City;
			q.append(" and Province like ? and Ctiy like ? and Area like ?", "%"+Province+"%", "%"+City+"%", "%"+Area+"%");
			DataTable dt = q.fetch();
			code = dt.getString(0, 0);
		} else {
			code = "000000";
		} 
		this.Response.put("Code", code);
	}
	
	
}
